package slidingWindow;

import org.junit.Test;

import java.util.ArrayList;

/*
 *@description 长度最小的子数组
 *@author hkfan
 *@version 1.0
 *@create 2025/5/28 14:06
 */
public class LeetCode_209 {

    @Test
    public void test(){
        System.out.println(minSubArrayLen(7,new int[]{2,3,1,2,4,3}));
    }

    public int minSubArrayLen(int target, int[] nums) {
        int res = solution_1(nums,target,0,0,0);
        if (res == Integer.MAX_VALUE){
            return 0;
        }
        return res;
    }



    private int solution_1(int[] nums, int target, int cur, int size, int index) {
        //1. 定义变量
        int sum=0,res=Integer.MAX_VALUE,l=0;
        //2. 循环
        for (int r = 0; r < nums.length; r++) {
            sum+=nums[r];
            if (sum>=target){
                while (sum>=target){
                    sum-=nums[l];
                    l++;
                }
                res = Math.min(res,r-l+2);
            }
        }
        //3. 返回结果
        return res;

    }



}
